//
//  YSRetweet.swift
//  新浪浪
//
//  Created by mac on 16/3/3.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit


class YSRetweet: UIView {

    //记录一个约束
    var bottomConstraint: Constraint?
    //定义同一个属性
    var retweetViewModel: YSStatusViewModel? {
        didSet{
            rewLabel.text = retweetViewModel?.retweetContent
            
            //数据这里来判断,如果有转发配图则显示转发配图,
            //没有抓发配图,则不显示配图
            //转发微博的图片都在转发微博的字段里
            //清空原有约束
            self.bottomConstraint?.uninstall()
            if let pic_url = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where pic_url.count>0{
                
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                //把model转换为ViewModel转换成ViewModel
                //定义一个临时数组进行数据拼接
                var result = [YSStatusViewModel]()
                for model in pic_url{
                    let viewModel = YSStatusViewModel()
                    viewModel.picture = model
                    result.append(viewModel)
                }
                pictureView.pictureList = result
            }else {
                pictureView.hidden = true
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(rewLabel.snp_bottom).constraint
                })
            }
            
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        backgroundColor = UIColor .grayColor()
        addSubview(rewLabel)
        addSubview(pictureView)
        
        rewLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
        }
        //默认认为 都有转发配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(CellMargin)
            make.top.equalTo(rewLabel.snp_bottom)
            //给他一个固定的宽高
            make.size.equalTo(CGSizeMake(100, 100))
        }
        //转发微博视图的底部在哪里
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    }
    
    private lazy var rewLabel:UILabel = {
        let lab = UILabel()
        
        lab.text = "转发微博"
        lab.textColor = UIColor .purpleColor()
        lab.font = UIFont.systemFontOfSize(15)
        lab.numberOfLines = 0
        
        return lab
    }()
    
    lazy var pictureView: YSPicture = YSPicture()
    
    
}
